/**    
 * @{#} CreateCommentActivity.java Create on 2014-6-10 下午1:35:57    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.o.app.json.JsonDate;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.util.OUtil;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.extra.QuestionAnswerExtra;
import com.smiier.skin.net.AnswerAppraiseSetTask;
import com.smiier.skin.net.AnswerAppraiseSetTask.AnswerAppraiseSetRequest;
import com.smiier.skin.net.AnswerAppraiseSetTask.AnswerAppraiseSetResponse;
import com.smiier.skin.net.entity.QuestionAnswer;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class CreateCommentActivity extends BasicActivity {

	private TextView text_question_content, text_question_create_date,
			text_answer_doctor, text_answer_date;

	private LinearLayout ll_comment_good, ll_comment_middle, ll_comment_bad,
			ll_pay_password;
	private LinearLayout[] comment_level = new LinearLayout[3];

	private int commentLevel = -1;
	private EditText edit_comment, edit_pay_password;
	private QuestionAnswer mQuestionAnswer;
	double cost = 0;
	private QuestionAnswerExtra mExtra;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_comment);
		mExtra = new QuestionAnswerExtra();
		mExtra.getFrom(getIntent());
		mQuestionAnswer = mExtra.getQuestionAnswer();
		if (mQuestionAnswer == null) {
			finish();
		}
		init();
		setNavTitle("创建评价");

		text_question_content = (TextView) findViewById(R.id.text_question_content);
		text_question_create_date = (TextView) findViewById(R.id.text_question_create_date);
		text_answer_doctor = (TextView) findViewById(R.id.text_answer_doctor);
		text_answer_date = (TextView) findViewById(R.id.text_answer_date);
		ll_pay_password = (LinearLayout) findViewById(R.id.ll_pay_password);
		try {

			text_question_content.setText(mQuestionAnswer.Question.Content);
			cost = mQuestionAnswer.Question.Cost;
			StringBuilder sb = new StringBuilder();
			JsonDate data = new JsonDate();
			sb.append("创建时间：")
					.append(CommonUtility.getTimeDiff(
							mQuestionAnswer.Question.CreateTime.toString(), 0,
							16)).append(" | ").append("悬赏：").append("￥")
					.append(cost);
			text_question_create_date.setText(sb);
			text_answer_doctor.setText("接单医生："
					+ mQuestionAnswer.Doctor_User.Name);
			text_answer_date.setText("接单时间："
					+ CommonUtility.getTimeDiff(
							mQuestionAnswer.Question.Doctor_Time.toString(), 0,
							16));
		} catch (Exception e) {
			e.printStackTrace();
		}

		ll_comment_good = (LinearLayout) findViewById(R.id.ll_comment_good);
		ll_comment_middle = (LinearLayout) findViewById(R.id.ll_comment_middle);
		ll_comment_bad = (LinearLayout) findViewById(R.id.ll_comment_bad);

		comment_level[0] = ll_comment_good;
		comment_level[1] = ll_comment_middle;
		comment_level[2] = ll_comment_bad;

		edit_comment = (EditText) findViewById(R.id.edit_comment);
		edit_pay_password = (EditText) findViewById(R.id.edit_pay_password);

		if (cost == 0) {
			ll_pay_password.setVisibility(View.GONE);
		}
	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.btn_submit_comment) {
			final String comment = CommonUtility.getText(edit_comment);
			String pay_password = CommonUtility.getText(edit_pay_password);
			if (-1 == commentLevel) {
				CommonUtility.tip(activity, "请选择评价");
				return;
			}
			if (CommonUtility.isNull(comment)) {
				CommonUtility.tip(activity, "请输入评价");
				return;
			}
			if (CommonUtility.isNull(pay_password) && cost > 0) {
				CommonUtility.tip(activity, "请输入您的支付密码");
				return;
			}
			AnswerAppraiseSetTask task = new AnswerAppraiseSetTask();
			AnswerAppraiseSetRequest req = new AnswerAppraiseSetRequest();
			req.Token = GlobalSettings.sToken;
			req.qid = mQuestionAnswer.Question.Qid;
			req.level = commentLevel;
			req.content = comment;
			req.PayPwd = OUtil.md5(pay_password);
			task.setRequest(req);
			task.addListener(new NetTaskListener<AnswerAppraiseSetTask.AnswerAppraiseSetRequest, AnswerAppraiseSetTask.AnswerAppraiseSetResponse>() {

				@Override
				public void onException(
						INetTask<AnswerAppraiseSetRequest, AnswerAppraiseSetResponse> task,
						Exception e) {

				}

				@Override
				public void onComplete(
						INetTask<AnswerAppraiseSetRequest, AnswerAppraiseSetResponse> task,
						AnswerAppraiseSetResponse response) {
					if (!CommonUtility.isNull(response)
							&& response.ResultCode == 200) {
						CommonUtility.toast(activity, "提交评价成功");
						Intent intent = new Intent(Constant.RECIVER_COMMENT);
						if (mExtra.putTo(intent)) {
							sendBroadcast(intent);
						}
						intent = new Intent(Constant.RECIVER_CHAT_LIST);
						sendBroadcast(intent);
						CommonUtility.finishActivityFromName(ChatActivity.class
								.getSimpleName());
					} else {
						CommonUtility.toast(activity, "提交评价失败");
					}
				}
			});
			add(task);

			/*
			 * HashMap<String, Object> params = new HashMap<String, Object>();
			 * params.put(Constant.PARAM_API, "Answer.Appraise.Set");
			 * params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken); try {
			 * params.put("qid", mQuestionAnswer.Question.Qid); } catch
			 * (Exception e) { e.printStackTrace(); } params.put("level",
			 * commentLevel); params.put("content", comment);
			 * params.put("PayPwd", OUtil.md5(pay_password));
			 * RequestTaskIntercept.requestIntercept(activity,
			 * Constant.URL.REQUEST, new RequestHandler(activity, new
			 * HandlerCallback() {
			 * 
			 * @Override public void callback(Object o) { if
			 * (!CommonUtility.isNull(o)) { JSONObject object = (JSONObject) o;
			 * if (CommonUtility.response200(object)) {
			 * CommonUtility.toast(activity, "提交评价成功"); try { JSONObject
			 * appraise = all .getJSONObject("Appraise"); appraise.put("Level",
			 * commentLevel); appraise.put("Content", comment);
			 * question.put("Is_Appraise", 1); all.put("Appraise", appraise);
			 * all.put("Question", question); Intent intent = new Intent(
			 * Constant.RECIVER_COMMENT); intent.putExtra(
			 * Constant.IDENTITY_KEY, all.toString()); sendBroadcast(intent);
			 * intent = new Intent( Constant.RECIVER_CHAT_LIST);
			 * sendBroadcast(intent); } catch (Exception e) {
			 * e.printStackTrace(); } CommonUtility
			 * .finishActivityFromName(ChatActivity.class .getSimpleName());
			 * finish(); } else { CommonUtility.toast(activity, "提交评价失败"); } } }
			 * }), params);
			 */
		}
	}

	public void onComment(View view) {
		resetCommentLevel(view);
	}

	void resetCommentLevel(View view) {
		for (LinearLayout layout : comment_level) {
			layout.setBackgroundResource(R.drawable.bg_right_border_normal);
		}
		view.setBackgroundColor(0xffaaaaaa);
		commentLevel = Integer.parseInt(view.getTag().toString());
	}
	
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}
*/
}
